package com.gm.wholesale.module.order.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.util.numberUtils.NumberUtils;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.wholesale.R;
import com.gm.wholesale.module.good.bean.GoodBean;

import java.util.List;

public class ChoiceGoodView extends LinearLayout {
    private Context mContext;
    private ListView mListView;
    public interface OnChoiceGoodListener{
        void onChoiceGood(GoodBean goodBean);
    }
    public ChoiceGoodView(Context context) {
        super(context);
        initView(context);
    }

    public ChoiceGoodView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_choice_good,this);
        mListView=findViewById(R.id.goodListView);
        findViewById(R.id.closeImage).setOnClickListener(view -> Popupwindow.hiddenPop());
    }

    public void showView(List<GoodBean> goodBeans,OnChoiceGoodListener listener){
        mListView.setAdapter(new CommonAdapter<>(mContext, goodBeans, R.layout.list_choice_good_item) {
            @Override
            public void convert(ViewHolder viewHolder, GoodBean goodBean) {
                viewHolder.setText(R.id.goodNameTv, goodBean.getName());
                viewHolder.setText(R.id.goodUnitTv, goodBean.getUnitName());
                viewHolder.setText(R.id.goodPriceTv, NumberUtils.getMoneyString(goodBean.getPrice()));

                viewHolder.getConvertView().setOnClickListener(view -> listener.onChoiceGood(goodBean));
            }
        });
    }




}
